package com.lkm.market2_0.m_imple.steelyardserver;

import com.lkm.frame.P;
import com.lkm.frame.task2.Task;
import com.lkm.frame.task2.TaskManager;
import com.lkm.hardware_connect.IHardwareControler;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m.steelyardserver.to.WeightChangeInfo;
import com.lkm.market2_0.m_imple.steelyardserver.server.sendtask.SendTitleWriteTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.sendtask.SetDateTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.sendtask.SetGoodsButtonTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.sendtask.SetSteelyardOkTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.sendtask.SetSteelyardRepairingTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.sendtask.SetWifiConfigTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.sendtask.UpRomTask;
import com.lkm.market2_0.m.steelyardserver.GoodsButtonInfoTo;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m.steelyardstatus.SteelyardStatusManagerModule;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsSendWriteTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.CMDEntity;
import com.lkm.market2_0.m_imple.steelyardserver.server.CMDFactory;
import com.lkm.market2_0.m_imple.steelyardserver.server.ClientSessionMap;
import com.lkm.market2_0.m_imple.steelyardserver.server.ICMDHandle;
import com.lkm.market2_0.m_imple.steelyardserver.server.ReceiveTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.sendtask.SetLockKeySwitchTask;
import com.lkm.market2_0.m_imple.steelyardserver.server.sendtask.SetPrintTask;
import com.lkm.market2_0.task.DefaultTaskExecutor;

import java.util.List;




public abstract class SteelyardServerModuleBase implements SteelyardServerModule {

    public SteelyardStatusManagerModule mSteelyardStatusManagerModule;



    public SteelyardServerModuleBase(SteelyardStatusManagerModule steelyardStatusManagerModule) {
        mSteelyardStatusManagerModule=steelyardStatusManagerModule;
        init();
    }





    protected abstract void init();



    public static Task<IHardwareControler, ?, ?> createReadTask(String clientSessionId, IHardwareControler hardwareControler, SteelyardStatusManagerModule mSteelyardStatusManagerModule,boolean isNio){

        String taskId= ReceiveTask.createId(clientSessionId);
        TaskManager taskManager= ExpandConfig.instance.getTaskManager2();

        
        int [] runTaskSers=taskManager.getRunTask(taskId);
        if(runTaskSers!=null){
            for (int taskSer:runTaskSers) {
                Task task = taskManager.getRunTask(taskSer);
                if(task!=null)task.cancel();
            }
        }

        
        
        ReceiveTask readTask=new ReceiveTask(new DefaultTaskExecutor<IHardwareControler, Object, Void>(true),taskManager,clientSessionId,mSteelyardStatusManagerModule, isNio){
            @Override
            protected ICMDHandle createICMDHandleByCMDEntity(CMDEntity cmdEntity) {
                return CMDFactory.createICMDHandleByCMDEntity(cmdEntity);
            }
        };

        if(P.isDebug())
            P.p("====接收到新的链接 ： "+clientSessionId);
        readTask.setId(taskId);
        readTask.setParam(hardwareControler);


        return readTask;

    }

    @Override
    public AbsSendWriteTask print(String steelyardMac, String table, String qr) {

        ReceiveTask receiveTask=findReceiveTaskBySteelyardMac(steelyardMac);


        if(receiveTask!=null){


            AbsSendWriteTask setPrintTask = null;

            IHardwareControler iHardwareControler= receiveTask.getIHardwareControler();

//            if(statusInfo!=null&& AbsSendWrite3134Task.isUseTheVersion(statusInfo.getVersionInt())){
//                setPrintTask=new SetPrint3134Task(new  DefaultTaskExecutor<Object[], Void, Boolean>(true),com.lkm.market2_0.Config.instance.getTaskManager2(),iHardwareControler,receiveTask,table,  qr);

                setPrintTask=new SetPrintTask(new  DefaultTaskExecutor<Object[], Void, Boolean>(true), ExpandConfig.instance.getTaskManager2(),iHardwareControler,receiveTask,table,  qr);



            setPrintTask.action();
            setPrintTask.execTask();
            return setPrintTask;
        }

        return null;
    }

    @Override
    public AbsSendWriteTask setSteelyardRepairing(String steelyardMac) {

        ReceiveTask receiveTask=findReceiveTaskBySteelyardMac(steelyardMac);
        if(receiveTask!=null){
            IHardwareControler iHardwareControler= receiveTask.getIHardwareControler();
            SetSteelyardRepairingTask setPrintTask=new SetSteelyardRepairingTask(new DefaultTaskExecutor<Object[], Void, Boolean>(true), ExpandConfig.instance.getTaskManager2(),iHardwareControler,receiveTask);
            setPrintTask.action();
            setPrintTask.execTask();
            return setPrintTask;
        }

        return null;
    }


    @Override
    public AbsSendWriteTask setSteelyardOK(String steelyardMac) {
        ReceiveTask receiveTask=findReceiveTaskBySteelyardMac(steelyardMac);
        if(receiveTask!=null){
            IHardwareControler iHardwareControler= receiveTask.getIHardwareControler();
            SetSteelyardOkTask setSteelyardOkTask=new SetSteelyardOkTask(new DefaultTaskExecutor<Object[], Void, Boolean>(true), ExpandConfig.instance.getTaskManager2(),iHardwareControler,receiveTask);
            setSteelyardOkTask.action();
            setSteelyardOkTask.execTask();
            return setSteelyardOkTask;
        }

        return null;
    }

    @Override
    public AbsSendWriteTask setGoodsButton(String steelyardMac, List<GoodsButtonInfoTo> goodsInfoTos) {
        ReceiveTask receiveTask=findReceiveTaskBySteelyardMac(steelyardMac);

        if(receiveTask!=null){
            IHardwareControler iHardwareControler= receiveTask.getIHardwareControler();
            AbsSendWriteTask setPrintTask=null;


//            if(statusInfo!=null&&SetGoodsButton3134Task.isUseTheVersion(statusInfo.getVersionInt())){
//                setPrintTask=new SetGoodsButton3134Task(new  DefaultTaskExecutor<Object[], Void, Boolean>(true),com.lkm.market2_0.Config.instance.getTaskManager2(),iHardwareControler,receiveTask, goodsInfoTos);

                setPrintTask=new SetGoodsButtonTask(new  DefaultTaskExecutor<Object[], Void, Boolean>(true), ExpandConfig.instance.getTaskManager2(),iHardwareControler,receiveTask, goodsInfoTos);


            setPrintTask.action();
            setPrintTask.execTask();
            return setPrintTask;
        }
        return null;
    }

    public AbsSendWriteTask setTitle(String steelyardMac,String title, String subTitle) {
        ReceiveTask receiveTask=findReceiveTaskBySteelyardMac(steelyardMac);

        if(receiveTask!=null){
            IHardwareControler iHardwareControler= receiveTask.getIHardwareControler();
            SendTitleWriteTask setPrintTask=new SendTitleWriteTask(new DefaultTaskExecutor<Object[], Void, Boolean>(true), ExpandConfig.instance.getTaskManager2(),iHardwareControler,receiveTask, title,subTitle);
            setPrintTask.action();
            setPrintTask.execTask();
            return setPrintTask;
        }
        return null;
    }
    public AbsSendWriteTask setWifiConfig(String steelyardMac,String wifiName, String wifiPass,String wifiModuleBaudRate, String ip, String port, String wifiModel) {
        ReceiveTask receiveTask=findReceiveTaskBySteelyardMac(steelyardMac);

        if(receiveTask!=null){
            IHardwareControler iHardwareControler= receiveTask.getIHardwareControler();

            SetWifiConfigTask setPrintTask=new SetWifiConfigTask(new  DefaultTaskExecutor<Object[], Void, Boolean>(true), ExpandConfig.instance.getTaskManager2(),iHardwareControler,receiveTask, wifiName,wifiPass,wifiModuleBaudRate,ip,port,wifiModel);
            setPrintTask.action();
            setPrintTask.execTask();
            return setPrintTask;
        }
        return null;
    }


    public static volatile boolean isLock = false;
    @Override
    public AbsSendWriteTask setLockKeySwitch(String steelyardMac,boolean isLock) {
        ReceiveTask receiveTask=findReceiveTaskBySteelyardMac(steelyardMac);
        SteelyardServerModuleBase.isLock=isLock;
        if(receiveTask != null){
            IHardwareControler iHardwareControler= receiveTask.getIHardwareControler();
            SetLockKeySwitchTask setPrintTask=new SetLockKeySwitchTask(new DefaultTaskExecutor<Object[], Void, Boolean>(true), ExpandConfig.instance.getTaskManager2(),iHardwareControler,receiveTask,isLock);
            setPrintTask.action();
            setPrintTask.execTask();
            return setPrintTask;
        }

        return null;
    }





    private ReceiveTask findReceiveTaskBySteelyardMac(String steelyardMac){

        String clientSessionId= ClientSessionMap.getInstance().getClientSessionIdBysteelyardMac(steelyardMac);

        TaskManager taskManager= ExpandConfig.instance.getTaskManager2();

        int[] ses=taskManager.getRunTask(ReceiveTask.createId(clientSessionId));

        ReceiveTask receiveTask=null;
        if(ses!=null){
            receiveTask = (ReceiveTask)taskManager.getRunTask(ses[0]);
        }
        return receiveTask;
    }



    @Override
    public AbsSendWriteTask tickOK_block(String steelyardMac){
        ReceiveTask receiveTask=findReceiveTaskBySteelyardMac(steelyardMac);


        if (P.isDebug()&&false) {
            String log="======回答ok "+(receiveTask!=null);
            P.p(log+steelyardMac);
            if(UIP.isPrint()){
                UIP.p(log,steelyardMac);
            }
        }
        if(receiveTask!=null) {
            receiveTask.tickOK();
        }

        


//            SentTickOkTask setPrintTask=new SentTickOkTask(new DefaultTaskExecutor<Object[], Void, Boolean>(true),com.lkm.market2_0.Config.instance.getTaskManager2(),iHardwareControler,receiveTask);




        return null;
    }


}
